package com.jiangxiaofei.citycloud.activity.freshfruits;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.FreshRecyTypeAdapter;
import com.jiangxiaofei.citycloud.adapter.NeedListAdapter;
import com.jiangxiaofei.citycloud.base.HaveNeedBaseActivity;
import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.bean.FreshFruitCassifyBean;
import com.jiangxiaofei.citycloud.bean.NeedBean;
import com.jiangxiaofei.citycloud.constants.BannerId;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FreshConstant;
import com.jiangxiaofei.citycloud.mvp.FreshPersenter;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.param.FleaNeedListParam;
import com.jiangxiaofei.citycloud.util.BannerViewHolder;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXFei
 * @date: 2017/11/24 0024
 * @content: 果蔬需求列表页面
 */

public class FreshFruitsNeedListActivity extends HaveNeedBaseActivity
        implements FreshConstant.GetFreshNeedListView, MainConstant.GetBannerView,
        FreshConstant.GetFreshCassifyView {
    @InjectView(R.id.img_back)
    ImageView imgBack;
    @InjectView(R.id.tv_search_type)
    TextView tvSearchType;
    @InjectView(R.id.et_search)
    TextView etSearch;
    @InjectView(R.id.linear_search)
    LinearLayout linearSearch;
    @InjectView(R.id.img_publish)
    TextView imgPublish;
    @InjectView(R.id.banner)
    MZBannerView banner;
    @InjectView(R.id.rela_btn1)
    RelativeLayout relaBtn1;
    @InjectView(R.id.rela_btn2)
    RelativeLayout relaBtn2;
    @InjectView(R.id.rela_btn3)
    RelativeLayout relaBtn3;
    @InjectView(R.id.recy_flea_need)
    RecyclerView recyFleaNeed;
    @InjectView(R.id.srf_layout)
    SmartRefreshLayout srfLayout;
    private NeedListAdapter needRecyAdapter;
    private List<NeedBean> freshNeedBean = new ArrayList<>();
    private FreshConstant.GetFreshNeedListPersenter getFreshNeedListPersenter;
    private MainConstant.GetBannerPersenter getBannerPersenter;
    private FreshConstant.GetFreshCassifyPersenter getFreshCassifyPersenter;
    private FleaNeedListParam param;
    private BDLocation bdLocation;
    private int typeId = 0;
    private String lng;
    private String lat;
    private int page = 1;
    private int issue = 1;
    private List<FreshFruitCassifyBean> cassifyBeans = new ArrayList<>();
    private FreshRecyTypeAdapter typeAdapter;
    private PopupWindow mTypePopupWindow;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_flea_market_need_layout);
        ButterKnife.inject(this);
        initSearchTitle();
        mContext = this;
        mActivity = this;
        initAdapter();
        param = new FleaNeedListParam();
        getFreshNeedListPersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshNeedListView) mActivity);
        getBannerPersenter = new MainPersenter(mContext, (MainConstant.GetBannerView) mActivity);
        initLocation();
        initParam();
        initPopup();
        initRefres();
        getFreshNeedListPersenter.getNeedList();
        getBannerPersenter.getBanner();
        getFreshCassifyPersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshCassifyView) mActivity);
        getFreshCassifyPersenter.getFreshCassify();
    }

    /**
     * 初始化刷新框架
     */
    private void initRefres() {
        srfLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
        srfLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
        srfLayout.setOnLoadmoreListener(refreshlayout -> {
            refreshlayout.finishLoadmore(2000);
            page++;
            param.setPage(page);
            getFreshNeedListPersenter.getNeedList();
        });
        srfLayout.setOnRefreshListener(refreshlayout -> {
            refreshlayout.finishRefresh(2000);
            page = 1;
            param.setPage(page);
            freshNeedBean.clear();
            getFreshNeedListPersenter.getNeedList();
        });
    }

    /**
     * 初始化请求参数
     */
    private void initParam() {
        param = new FleaNeedListParam();
        param.setPage(page);
        param.setId(typeId);
        param.setIssue(issue);
        param.setLat(lat);
        param.setLng(lng);
    }

    private void initLocation() {
        if (EmptyUtils.isEmpty(Constants.bdLocation)) {
            Constants.bdclient.start();
        }
        bdLocation = Constants.bdLocation;
        lat = String.valueOf(bdLocation.getLatitude());
        lng = String.valueOf(bdLocation.getLongitude());
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        needRecyAdapter = new NeedListAdapter(R.layout.item_flea_need_layout, freshNeedBean);
        needRecyAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, FreshFruitsNeedDetailActivity.class);
            intent.putExtra(StringConstants.FRESHNEEDID, Integer.valueOf(freshNeedBean.get(position).getId()));
            startActivity(intent);
        });
        recyFleaNeed.setLayoutManager(linearLayoutManager);
        recyFleaNeed.setAdapter(needRecyAdapter);
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public FleaNeedListParam getNeedListParam() {
        return param;
    }

    @Override
    public void getNeedListSuccess(List<NeedBean> bean) {
        if (page == 1) {
            freshNeedBean.clear();
        }
        freshNeedBean.addAll(bean);
        needRecyAdapter.notifyDataSetChanged();
    }

    @Override
    public int getBannerParam() {
        return BannerId.FRESHNEEDID;
    }

    @Override
    public void bannerSuccess(List<BannerBean> bannerBeans) {
        banner.setPages(bannerBeans, new BannerViewHolder(mContext));
        banner.start();
    }

    /**
     * 初始化分类popupWindow
     */
    private void initPopup() {
        mTypePopupWindow = new PopupWindow(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_flea_type_item_layout, null);
        mTypePopupWindow.setContentView(view);
        mTypePopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mTypePopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mTypePopupWindow.setFocusable(true);
        mTypePopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        RecyclerView recyType = view.findViewById(R.id.recy_type_item);
        typeAdapter = new FreshRecyTypeAdapter(R.layout.item_flea_popup_text_layout, cassifyBeans);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyType.setLayoutManager(linearLayoutManager);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < cassifyBeans.size(); i++) {
                    cassifyBeans.get(i).setChecked(false);
                }
                cassifyBeans.get(position).setChecked(true);
                typeAdapter.notifyDataSetChanged();
                typeId = Integer.valueOf(cassifyBeans.get(position).getId());
                param.setId(typeId);
                page = 1;
                param.setPage(page);
                getFreshNeedListPersenter.getNeedList();
                mTypePopupWindow.dismiss();
            }
        });
        recyType.setAdapter(typeAdapter);
    }

    @Override
    public int getFreshCassifyId() {
        return typeId;
    }

    @Override
    public void getFreshCassifyOnSuccess(List<FreshFruitCassifyBean> cassifyBeans) {
        this.cassifyBeans.clear();
        this.cassifyBeans.addAll(cassifyBeans);
        typeAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.rela_btn2})
    public void bOnClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.rela_btn1:
                //默认

                break;
            case R.id.rela_btn2:
                //分类
                mTypePopupWindow.showAsDropDown(v);
                break;
            case R.id.rela_btn3:
                //发布时间
                issue = 1;
                break;
        }
    }
}

